{
 "cells": [
  {
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-14T07:47:37.029079Z",
     "start_time": "2025-01-14T07:47:36.655508Z"
    }
   },
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-14T08:12:58.216445Z",
     "start_time": "2025-01-14T08:12:58.209728Z"
    }
   },
   "cell_type": "code",
   "source": [
    "index1 = pd.MultiIndex.from_arrays([['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'd'],\n",
    "                [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]], names=['cloth', 'size'])\n",
    "\n",
    "ser_obj = pd.Series(np.random.randn(12),index=index1)\n",
    "print(ser_obj)\n",
    "print('-'*50)\n",
    "print(type(ser_obj)) \n",
    "print('-'*50)\n",
    "print(type(ser_obj.index)) \n",
    "print('-'*50)\n",
    "print(ser_obj.index)\n",
    "print('-'*50)\n",
    "print(ser_obj.index.levels) \n",
    "print('-'*50)\n",
    "ser_obj.index.codes "
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cloth  size\n",
      "a      0      -0.645085\n",
      "       1      -0.231491\n",
      "       2       0.967451\n",
      "b      0       0.846271\n",
      "       1       1.375963\n",
      "       2      -2.271110\n",
      "c      0       0.447755\n",
      "       1       0.331932\n",
      "       2       2.143399\n",
      "d      0       0.578914\n",
      "       1      -0.548841\n",
      "       2      -2.000195\n",
      "dtype: float64\n",
      "--------------------------------------------------\n",
      "<class 'pandas.core.series.Series'>\n",
      "--------------------------------------------------\n",
      "<class 'pandas.core.indexes.multi.MultiIndex'>\n",
      "--------------------------------------------------\n",
      "MultiIndex([('a', 0),\n",
      "            ('a', 1),\n",
      "            ('a', 2),\n",
      "            ('b', 0),\n",
      "            ('b', 1),\n",
      "            ('b', 2),\n",
      "            ('c', 0),\n",
      "            ('c', 1),\n",
      "            ('c', 2),\n",
      "            ('d', 0),\n",
      "            ('d', 1),\n",
      "            ('d', 2)],\n",
      "           names=['cloth', 'size'])\n",
      "--------------------------------------------------\n",
      "[['a', 'b', 'c', 'd'], [0, 1, 2]]\n",
      "--------------------------------------------------\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "FrozenList([[0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "cell_type": "code",
   "source": [
    "print(ser_obj['c']) \n",
    "print('-'*50)\n",
    "print(ser_obj.loc['a', 2])\n",
    "print('-'*50)\n",
    "print(ser_obj[:, 2]) "
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-14T08:15:12.026079Z",
     "start_time": "2025-01-14T08:15:12.021558Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "size\n",
      "0    0.447755\n",
      "1    0.331932\n",
      "2    2.143399\n",
      "dtype: float64\n",
      "--------------------------------------------------\n",
      "0.9674511583931438\n",
      "--------------------------------------------------\n",
      "cloth\n",
      "a    0.967451\n",
      "b   -2.271110\n",
      "c    2.143399\n",
      "d   -2.000195\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "cell_type": "code",
   "source": [
    "print(ser_obj.swaplevel())\n",
    "print('-'*50)\n",
    "print(ser_obj)\n",
    "print('-'*50)\n",
    "ser_obj=ser_obj.swaplevel()\n",
    "print(ser_obj)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-14T08:18:56.417139Z",
     "start_time": "2025-01-14T08:18:56.412129Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "size  cloth\n",
      "0     a       -0.645085\n",
      "1     a       -0.231491\n",
      "2     a        0.967451\n",
      "0     b        0.846271\n",
      "1     b        1.375963\n",
      "2     b       -2.271110\n",
      "0     c        0.447755\n",
      "1     c        0.331932\n",
      "2     c        2.143399\n",
      "0     d        0.578914\n",
      "1     d       -0.548841\n",
      "2     d       -2.000195\n",
      "dtype: float64\n",
      "--------------------------------------------------\n",
      "cloth  size\n",
      "a      0      -0.645085\n",
      "       1      -0.231491\n",
      "       2       0.967451\n",
      "b      0       0.846271\n",
      "       1       1.375963\n",
      "       2      -2.271110\n",
      "c      0       0.447755\n",
      "       1       0.331932\n",
      "       2       2.143399\n",
      "d      0       0.578914\n",
      "       1      -0.548841\n",
      "       2      -2.000195\n",
      "dtype: float64\n",
      "--------------------------------------------------\n",
      "size  cloth\n",
      "0     a       -0.645085\n",
      "1     a       -0.231491\n",
      "2     a        0.967451\n",
      "0     b        0.846271\n",
      "1     b        1.375963\n",
      "2     b       -2.271110\n",
      "0     c        0.447755\n",
      "1     c        0.331932\n",
      "2     c        2.143399\n",
      "0     d        0.578914\n",
      "1     d       -0.548841\n",
      "2     d       -2.000195\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-14T08:33:26.598859Z",
     "start_time": "2025-01-14T08:33:26.595376Z"
    }
   },
   "cell_type": "code",
   "source": "print(ser_obj.sort_index(level=0)) ",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "size  cloth\n",
      "0     a       -0.645085\n",
      "      b        0.846271\n",
      "      c        0.447755\n",
      "      d        0.578914\n",
      "1     a       -0.231491\n",
      "      b        1.375963\n",
      "      c        0.331932\n",
      "      d       -0.548841\n",
      "2     a        0.967451\n",
      "      b       -2.271110\n",
      "      c        2.143399\n",
      "      d       -2.000195\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "cell_type": "code",
   "source": [
    "df_obj=ser_obj.unstack() \n",
    "print(df_obj)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-14T08:36:24.866963Z",
     "start_time": "2025-01-14T08:36:24.861963Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cloth         a         b         c         d\n",
      "size                                         \n",
      "0     -0.645085  0.846271  0.447755  0.578914\n",
      "1     -0.231491  1.375963  0.331932 -0.548841\n",
      "2      0.967451 -2.271110  2.143399 -2.000195\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "cell_type": "code",
   "source": "print(df_obj.stack()) ",
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-14T08:37:11.936517Z",
     "start_time": "2025-01-14T08:37:11.932884Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "size  cloth\n",
      "0     a       -0.645085\n",
      "      b        0.846271\n",
      "      c        0.447755\n",
      "      d        0.578914\n",
      "1     a       -0.231491\n",
      "      b        1.375963\n",
      "      c        0.331932\n",
      "      d       -0.548841\n",
      "2     a        0.967451\n",
      "      b       -2.271110\n",
      "      c        2.143399\n",
      "      d       -2.000195\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
